package com.example.barcodeapp.ui.wode.fragment;


import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;


import com.example.barcodeapp.R;
import com.example.barcodeapp.adapter.FragmentAdapter;
import com.example.barcodeapp.base.BaseFragment;
import com.example.barcodeapp.interfaces.IBasePersenter;
import com.example.barcodeapp.ui.wode.bean.DingDanBean;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PaymentFragment extends BaseFragment {
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private OrderformFragmentAdapter adapter;
    private List<DingDanBean.DataBean> list;
    private ArrayList<DingDanBean.DataBean> allBean;
    private ArrayList<DingDanBean.DataBean> obligtionBean;
    private ArrayList<DingDanBean.DataBean> offTheStocksBean;
    public PaymentFragment(List<DingDanBean.DataBean> list) {
        this.list = list;
    }

    @Override
    protected int getLayout() {
        return R.layout.pay_layout1;
    }

    @Override
    protected void initView() {
        allBean = new ArrayList<>();
        obligtionBean = new ArrayList<>();
        offTheStocksBean = new ArrayList<>();
        setDate();
        allBean.addAll(obligtionBean);
        allBean.addAll(offTheStocksBean);
        ArrayList<Fragment> list = new ArrayList<>();
        list.add(new AllFragment2(allBean));//全部
        list.add(new ObligationFragment2(obligtionBean));//待付款
        list.add(new OffTheStocksFragment2(offTheStocksBean));//已完成
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getActivity().getSupportFragmentManager(), list);
        viewPager.setAdapter(fragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setText("全部");
        tabLayout.getTabAt(1).setText("待付款");
        tabLayout.getTabAt(2).setText("已完成");
    }

    private void setDate() {
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                String type = list.get(i).getType();
                if (type.equals("huodong")) {//表示是课程订单
                    int state = list.get(i).getState();
                    if (state == 1) {
                        obligtionBean.add(list.get(i));
                    } else if (state == 2) {
                        offTheStocksBean.add(list.get(i));
                    } else {
                        allBean.add(list.get(i));
                    }
                }
            }
        }
    }

    @Override
    protected IBasePersenter initPersenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    private class OrderformFragmentAdapter extends FragmentStatePagerAdapter {
        private ArrayList<Fragment> list;

        public OrderformFragmentAdapter(@NonNull FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
